#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>

static int task(size_t line, char *action, char *nav[]) {
        pid_t pid = fork();
        switch (pid) {
        default:
                break;
        case -1:
                perror("Fail to fork task");
                fprintf(stderr, "%s@%s:%ld->%d\n", __FILE__, action, line, __LINE__);
                exit(10);
        case 0:
                if (setuid(0) != -1)
                        execv("/usr/bin/pacman", nav);
                perror("Call fail");
                fprintf(stderr, "%s@%s:%ld->%d\n", __FILE__, action, line, __LINE__);
                exit(1);
        }
        int stat;
        wait(&stat);
        return stat;
}

int main(void) {
        char *sy[] = {"pacman", "-Sy", NULL};
        char *su[] = {"pacman", "-Su", "--noconfirm", NULL};
        int qc;
        do {
                qc = task(__LINE__, "-Sy", sy);
                if (qc)
                        sleep(5);
        } while (qc);
        do {
                qc = task(__LINE__, "-Su", su);
                if (qc)
                        sleep(5);
        } while (qc);
        puts("Done!");
}
